<script setup lang="ts">
defineOptions({
    name: 'OrderPay'
})
import { createMedicalOrderAPI, getAddressListAPI, getMedicalOrderPreAPI } from '@/apis/order';
import type { AddressItem, OrderPre } from '@/types/order';
import { showToast } from 'vant';
import { onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';

// 保存预支付的信息
const orderPre = ref<OrderPre>()
const route = useRoute()
// 获取预支付的信息
const getMedicalOrderPre = async () => {
    const res = await getMedicalOrderPreAPI({
        prescriptionId: route.query.id as string
    })
    // console.log(res)
    orderPre.value = res.data
}

// 保存收货地址信息
const address = ref<AddressItem>()
// 获取收货地址信息
const getAddressList = async () => {
    const res = await getAddressListAPI()
    // 如果有收货地址 并且里面有默认的收货地址
    if (res.data.length) {
        const defaultAddress = res.data.find((item) => item.isDefault === 1)
        if (defaultAddress) {
            address.value = defaultAddress
        } else {
            // 如果收货地址列表里面没有默认的收货地址 默认为数组的第一个元素是默认的收货地址
            address.value = res.data[0]
        }
    }
}

onMounted(() => {
    getMedicalOrderPre()
    getAddressList()
})

// 勾选是否我同意协议
const isArgee = ref(false)
const loading = ref(false)
const orderId = ref('')
const show = ref(false)
const onSubmit = async () => {
    // 判断用户是否勾选了 我同意协议
    if (!isArgee.value) return showToast('请勾选我同意协议')
    if (!address.value?.id) return showToast('请选择收货地址')
    if (!orderPre.value?.id) return showToast('没有处方ID')
    if (!orderId.value) {
        try {
            // 开启loading效果
            loading.value = true
            // 发送请求生成订单
            const res = await createMedicalOrderAPI({
                id: orderPre.value.id,
                couponId: orderPre.value.couponId,
                addressId: address.value.id
            })
            // console.log(res)
            orderId.value = res.data.id
            // 打开抽屉组件
            show.value = true
            // 关闭loading效果
            loading.value = false
        } catch (error) {
            loading.value = false
        }
    } else {
        show.value = true
    }

}
</script>

<template>
    <div class="order-pay-page" v-if="address && orderPre">
        <cp-nav-bar title="药品支付" />
        <div class="order-address">
            <p class="area">
                <van-icon name="location" />
                <span>{{ address.province + address.city + address.county }}</span>
            </p>
            <p class="detail">{{ address.addressDetail }}</p>
            <p>
                {{ address.receiver }} {{ address.mobile.replace(/^(\d{3})\d+(\d{4})$/, '$1****$2') }}</p>
        </div>
        <!-- TODO: 使用组件 药品订单详情 -->
        <MedicalOrderDetail :medicines="orderPre.medicines" />
        <div class="order-detail">
            <van-cell-group>
                <van-cell title="药品金额" :value="`￥${orderPre.payment}`" />
                <van-cell title="运费" :value="`￥${orderPre.expressFee}`" />
                <van-cell title="优惠券" :value="`-￥${orderPre.couponDeduction}`" />
                <van-cell title="实付款" :value="`￥${orderPre.actualPayment}`" class="price" />
            </van-cell-group>
        </div>
        <div class="order-tip">
            <p class="tip">
                由于药品的特殊性，如非错发、漏发药品的情况，药品一经发出
                不得退换，请核对药品信息无误后下单。
            </p>
            <van-checkbox v-model="isArgee">我已同意<a href="javascript:;">支付协议</a></van-checkbox>
        </div>
        <van-submit-bar :price="orderPre.actualPayment * 100" button-text="立即支付" button-type="primary" text-align="left"
            @click="onSubmit"></van-submit-bar>
    </div>
    <!-- 骨架屏 -->
    <div class="order-pay-page" v-else>
        <cp-nav-bar title="药品支付" />
        <van-skeleton title avatar :row="2" style="margin-top: 15px" />
        <van-skeleton title :row="4" style="margin-top: 30px" />
        <van-skeleton title :row="4" style="margin-top: 30px" />
    </div>
</template>

<style lang="scss" scoped>
:deep(.van-nav-bar) {
    background-color: var(--cp-primary);

    .van-nav-bar__arrow,
    .van-nav-bar__title {
        color: #fff;
    }
}

:deep(.van-cell) {
    .van-cell__title {
        font-size: 16px;
    }

    .van-cell__value {
        font-size: 16px;
    }

    &.price {
        .van-cell__value {
            font-size: 18px;
            color: var(--cp-price);
        }
    }
}

:deep(.van-submit-bar) {
    box-shadow: 0 0 2px rgba(0, 0, 0, 0.1);

    .van-button {
        width: 200px;
    }
}

.order-pay-page {
    padding: 46px 0 65px;
}

.order-address {
    padding: 15px 15px 0 15px;
    background-color: #fff;
    font-size: 13px;

    .area {
        color: var(--cp-tag);
        margin-bottom: 5px;

        .van-icon-location {
            color: #ff7702;
            font-size: 14px;
        }
    }

    .detail {
        font-size: 17px;
        margin-bottom: 5px;
    }

    &::after {
        content: '';
        display: block;
        height: 12px;
        background-color: var(--cp-bg);
        margin: 0 -15px;
        margin-top: 15px;
    }
}

.order-tip {
    padding: 0 15px;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    margin-top: 10px;

    .tip {
        font-size: 12px;
        color: var(--cp-tag);
        width: 100%;

        &::before {
            content: '*';
            color: var(--cp-price);
            font-size: 14px;
        }

        // margin-bottom:30px;
    }

    .van-checkbox {
        a {
            color: var(--cp-primary);
        }
    }
}
</style>
